package com.ge.modules.api.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ge.common.annotation.LoginUserApi;
import com.ge.common.utils.DeleteType;
import com.ge.common.validator.Assert;
import com.ge.modules.api.apiDTO.TbUserDto;
import com.ge.modules.api.service.*;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.entity.TbPostEntity;
import com.ge.modules.entity.TbTechnicianEntity;
import com.ge.modules.entity.TbUser;
import com.ge.modules.entity.TbUserConcernEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 普通户用
 *
 * @author wuye
 * @date 2018/1/17
 */
@RestController
@RequestMapping("/api/user")
public class ApiTbUserController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private TbUserTokenService tokenService;

    @Autowired
    private TbPostService tbPostService;

    @Autowired
    private TbUserConcernService tbUserConcernService;

    @Autowired
    private TbTechnicianService tbTechnicianService;

    /**
     * 查询列表
     *
     * @param params
     * @returnapi
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = tbUserService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    /**
     * 编辑用户信息
     *
     * @param tbUser
     * @param user
     * @return
     */
    @PostMapping("/save")
    public R save(@LoginUserApi TbUser tbUser, TbUser user) {
        user.setUserId(tbUser.getUserId());
        tbUserService.update(user);
        return R.ok();
    }

    /**
     * 修改普通用户
     *
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbUser tbUser) {
        tbUserService.update(tbUser);
        return R.ok();
    }


    /**
     * 删除普通用户(软删除)
     *
     * @param id
     * @return
     */
    public R delete(@RequestParam("id") int id) {
        tbUserService.deleteById(id);
        return R.ok();
    }


    /**
     * 通过uid获取个人资料
     *
     * @return
     */
    @RequestMapping("/infouid")
    public R infoByuid(Integer uid) {
        TbUser tbUser = tbUserService.queryById(uid);
        return R.ok().put("data", tbUser);
    }

    /**
     * 查询个人详细信息
     *
     * @param tbUser
     * @return
     */
    @GetMapping("userInfo")
    public R userInfo(@LoginUserApi TbUser tbUser) {
        TbUserDto tbUserDto = tbUser.toDto();
        //查询我的帖子列表
        int posts = 0;
        Map<String, Object> postMap = new HashMap<>();
        postMap.put("userId", tbUser.getUserId());
        List<TbPostEntity> tbPostEntityList = tbPostService.queryList(postMap);
        posts = tbPostEntityList.size();
        //查询关注和粉丝数量
        int concerns = 0;
        int fans = 0;
        //关注数 根据被关注用户id
        Map<String, Object> concernMap = new HashMap<>();
        concernMap.put("userId", tbUser.getUserId());
        List<TbUserConcernEntity> tbUserConcernEntityList = tbUserConcernService.queryList(concernMap);
        concerns = tbUserConcernEntityList.size();
        //粉丝数量
        Map<String, Object> fansMap = new HashMap<>();
        fansMap.put("byUserId", tbUser.getUserId());
        List<TbUserConcernEntity> tbUserConcernEntityListTwo = tbUserConcernService.queryList(fansMap);
        fans = tbUserConcernEntityListTwo.size();
        //
        tbUserDto.setPosts(posts);
        tbUserDto.setConcerns(concerns);
        tbUserDto.setFans(fans);
        return R.ok().put("data", tbUserDto);
    }

    /**
     * 用户申请成为技师
     *
     * @return
     */
    @GetMapping("userToBeTechnician")
    public R userToBeTechnician(@LoginUserApi TbUser tbUser) {
        Assert.isNull(tbUser.getHeadPicture(),"请设置头像 再申请");
        Assert.isNull(tbUser.getSex(),"请设置性别 再申请");
        Assert.isNull(tbUser.getBirthday(),"请设置生日 再申请");
        Assert.isNull(tbUser.getSignature(),"请设置签名 再申请");
        if(tbTechnicianService.queryByMobile(tbUser.getMobile())!=null){
            return R.error("已经是技师不要重复申请！");
        }
        TbTechnicianEntity tbTechnicianEntity = new TbTechnicianEntity();
        tbTechnicianEntity.setMobile(tbUser.getMobile());
        tbTechnicianEntity.setPassword(tbUser.getPassword());
        tbTechnicianEntity.setDeleteType(DeleteType.NOT_DELETE.getKey());
        tbTechnicianEntity.setCreateTime(new Date());
        tbTechnicianEntity.setHeadpicture(tbUser.getHeadPicture());
        tbTechnicianEntity.setSex(tbUser.getSex());
        tbTechnicianEntity.setBirthday(tbUser.getBirthday());
        tbTechnicianEntity.setSignature(tbUser.getSignature());
        tbTechnicianEntity.setRealname(tbUser.getNickname());
        tbTechnicianEntity.setNickname(tbUser.getNickname());
        tbTechnicianEntity.setUserType(2);
        tbTechnicianService.save(tbTechnicianEntity);
        return R.ok("恭喜你成为了平台技师 你可以加入店铺了！");
    }

}
